package com.briup.estore.dao.impl;

import com.briup.estore.common.pojo.Order;
import com.briup.estore.common.utils.JDBCUtils;
import com.briup.estore.dao.OrderDao;

import java.sql.Timestamp;
import java.util.List;

/**
 * @Author briup-adam
 * @Date 2023/8/3 下午3:02
 * @Description 对表【es_order】的数据库操作
 **/

public class OrderDaoImpl implements OrderDao {
    @Override
    public Order selectById(String orderId) {
        Order order = JDBCUtils.findOne("select id,user_id,create_date,pay_way,tracking_number,distribution_mode,status,pay_date,receive_username,receive_address,receive_phone from es_order where id=?;", Order.class, orderId);
        return order;
    }

    @Override
    public void updateStatusToClose(String orderId) {
        JDBCUtils.DML("update es_order set status =5 where id=?;", orderId);

    }

    @Override
    public void save(Order order) {
        JDBCUtils.DML("insert into es_order(id,user_id,create_date,status,receive_username,receive_address,receive_phone) values(?,?,?,?,?,?,?)", order.getId(), order.getUserId(), order.getCreateDate(), order.getStatus(), order.getReceiveUsername(), order.getReceiveAddress(), order.getReceivePhone());

    }

    @Override
    public List<Order> selectOrderDetailByUserId(String orderId, int orderStatus, Integer userId) {
        List<Order> list = null;
        String sql = "select id,user_id,create_date,pay_way,tracking_number,distribution_mode,status,pay_date,receive_username,receive_address,receive_phone from es_order where user_id=? and id like ?";
        if (orderStatus != 0) {
            sql += " and status =?;";
            list = JDBCUtils.findMany(sql, Order.class, userId, "%" + orderId + "%", orderStatus);
        } else {
            sql += ";";
            list = JDBCUtils.findMany(sql, Order.class, userId, "%" + orderId + "%");
        }

        return list;
    }

    @Override
    public void paySuccess(String orderId, Timestamp payDate, String payWay) {
        JDBCUtils.DML("update es_order set status = 2, pay_date = ?, pay_way =? where id=?", payDate, payWay, orderId);

    }

    @Override
    public void completeOrder(String orderId) {
        JDBCUtils.DML("update es_order set status = 4  where id=?", orderId);

    }
}
